package com.metacube.questionbank.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.metacube.questionbank.model.Question;
import com.metacube.questionbank.model.Tag;
import com.metacube.questionbank.model.User;
import com.metacube.questionbank.service.impl.AnswerServiceImpl;
import com.metacube.questionbank.service.impl.QuestionServiceImpl;
import com.metacube.questionbank.service.impl.TagServiceImpl;
import com.metacube.questionbank.service.impl.UserServiceImpl;

/*
 * @author Team Neeraj Jain
 * */
/*
 * This controller handles requests that comes for questions page.
 * */
@Controller
public class QuestionController {
	@Autowired
	QuestionServiceImpl questionService;
	@Autowired
	UserServiceImpl userService;
	@Autowired
	AnswerServiceImpl answerService;
	@Autowired 
	TagServiceImpl tagService;

	/*
	 * @param req This method handles the request that comes for the questions
	 * page.The view that it returns contains all the questions that are asked
	 * by the any user. The view also contains the number of answers for each
	 * question,the user who posted this question,the posting time and the
	 * related tags.
	 * 
	 * @return  view of questions page.
	 */
	@RequestMapping(value = "questions.htm")
	public String showQuestions(HttpServletRequest req) {
		List<Question> questions = questionService.getQuestions();
		List<Integer> answers = answerService.getAnswerCount(questions);
		List<List<Tag>> tags = tagService.getQuestionTags(questions);
		List<User> userList = userService.getUserQId(questions);
		req.setAttribute("questions", questions);
		req.setAttribute("ansCount", answers);
		req.setAttribute("tags", tags);
		req.setAttribute("userList", userList);
		return "questions";
	}

	/*
	 * @param req This method handles the request that comes for the unanswered
	 * questions.The view that it returns contains all the questions that are
	 * asked by the any user. The view also contains the number of answers for
	 * each question,the user who posted this question,the posting time and the
	 * related tags.
	 * 
	 * @return view of questions page.
	 */
	@RequestMapping(value = "unAnswered.htm")
	public String showUnAnsweredQuestions(HttpServletRequest req) {
		List<Question> questions = questionService.getUnAnsweredQuestions();
		List<Integer> answers = answerService.getAnswerCount(questions);
		List<List<Tag>> tags = tagService.getQuestionTags(questions);
		List<User> userList = userService.getUserQId(questions);
		req.setAttribute("questions", questions);
		req.setAttribute("ansCount", answers);
		req.setAttribute("tags", tags);
		req.setAttribute("userList", userList);
		return "questions";
	}
}